//
//  ViewController.m
//  LGNetworkDemo
//
//  Created by dangwc on 2019/5/15.
//  Copyright © 2019 dangwc. All rights reserved.
//

#import "ViewController.h"
#import "LGDownloadManager.h"
#import "LGNetworkingManager.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *testView;
@property (weak, nonatomic) IBOutlet UILabel *downLoadProgressLb;

@property (nonatomic,strong) LGDownloadManager *downloadManager;

@end

@implementation ViewController


-(LGDownloadManager *)downloadManager{
    
    if (!_downloadManager) {
        _downloadManager = [[LGDownloadManager alloc] initWithDownloadModel:TSDownloadModeBreakpointResume];
    }
    return _downloadManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
}
- (IBAction)beginDownload:(UIButton *)sender {
    
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
    //示例下载链接(后台要支持断点续传)
    NSString *downUrl = @"http://139.9.77.198:15000/Paper/P20180203162019663.atxz";
    
    NSString *path_sandox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];
    __weak typeof(self) weak_self = self;
    NSLog(@"缓存路径：%@",path_sandox);
    
    NSString *document2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    document2 = [NSString stringWithFormat:@"%@/TestPaper/%@",document2,@"P20180203162019663"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:document2]) {
        self.downLoadProgressLb.text = @"下载完成";
        return;
    }
    
    
    [self.downloadManager downloadDataWithRequestUrl:downUrl filesPath:path_sandox fileID:@"P20180203162019663.atxz" progress:^(double progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            weak_self.downLoadProgressLb.text = [NSString stringWithFormat:@"当前下载进度%.0f%%",100.0 * progress];
        });
    } seccess:^(NSURL *filePath) {
        NSLog(@"下载完成");
        weak_self.downLoadProgressLb.text = @"下载完成";
    } failure:^(NSError *error) {
        NSLog(@"下载异常%@",error.localizedDescription);
    }];
        
    }else{
        [self.downloadManager cancelDownload];
    }
    
}

- (IBAction)getRequest:(UIButton *)sender {
    
    NSString *urlString = @"https://mobile.ximalaya.com/discovery-firstpage/guessYouLike/list/ts-1534815616591";
    NSDictionary *parameters = @{@"appid":@"0",
                                 @"device":@"iPhone",
                                 @"deviceId":@"5DC0EF2A-01F6-41D1-8455-C4A1BF927E36",
                                 @"inreview":@"false",
                                 @"network":@"WIFI",
                                 @"operator":@"3",
                                 @"pageId":@"1",
                                 @"pageSize":@"5"
                                 };
    [LGNetworkingManager GETRequestUrl:urlString parameters:parameters success:^(id _Nonnull response) {
        NSLog(@"%@",response);
        NSData *data = [NSJSONSerialization dataWithJSONObject:response options:NSJSONWritingPrettyPrinted error:nil];
        self.testView.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
- (IBAction)postRequest:(UIButton *)sender {
    
    NSString *urlString = @"https://www.apiopen.top/satinCommentApi";
    NSDictionary *parameters = @{@"id":@"27610708",
                                 @"page":@"1"
                                 };
    [LGNetworkingManager POSTRequestUrl:urlString parameters:parameters success:^(id _Nonnull response) {
        NSLog(@"%@",response);
        NSData *data = [NSJSONSerialization dataWithJSONObject:response options:NSJSONWritingPrettyPrinted error:nil];
        self.testView.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

@end
